//
//  ImageViewController.m
//  QuickReadComic
//
//  Created by 苗钧豪 on 16/9/23.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "ImageViewController.h"
#import "UIImageView+WebCache.h"
@interface ImageViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *slideScrollView;
@property(nonatomic,strong)UIScrollView *zoomScrollView;
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,assign)CGFloat number;
@end

@implementation ImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _number = 0.0;
    if (_feeds.content.images.count == 0) {
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"高能预警" message:@"前方并没有小黄图" delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil];
        [alertView show];
    }
    self.imagesArray = [NSMutableArray array];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:0];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    UIImage *image = [UIImage imageNamed:@"返回 (1)"];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[image imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = item;
    self.view.backgroundColor = [UIColor darkGrayColor];
    _slideScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    _slideScrollView.contentSize = CGSizeMake(self.view.bounds.size.width * _imageNumber, self.view.bounds.size.height - 64);
    _slideScrollView.contentOffset = CGPointMake(0, 0);
    _slideScrollView.pagingEnabled = YES;
    _slideScrollView.bounces = NO;
    _slideScrollView.delegate = self;
    for (int i = 0; i < _imageNumber; i ++) {
        _zoomScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(i * self.view.bounds.size.width, - 64, self.view.bounds.size.width, self.view.bounds.size.height)];
        _zoomScrollView.tag = i;
        _zoomScrollView.delegate = self;
        _zoomScrollView.maximumZoomScale = 2.0;
        _zoomScrollView.minimumZoomScale = 0.5;
        [_slideScrollView addSubview:_zoomScrollView];
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        [_imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[_feeds.content.image_base stringByAppendingString:_feeds.content.images[i]]]]];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_zoomScrollView addSubview:_imageView];
    }
    [self.view addSubview:_slideScrollView];
    UITextView *textview = [[UITextView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height - 60, self.view.bounds.size.width, 60)];
    textview.backgroundColor = [UIColor blackColor];
    textview.alpha = 0.5;
    textview.editable = NO;
    textview.scrollEnabled = YES;
    textview.textColor = [UIColor whiteColor];
    textview.font = [UIFont systemFontOfSize:15];
    textview.text = _feeds.content.text;
    [self.view addSubview:textview];
}
- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    _imageView = scrollView.subviews.firstObject;
    return _imageView;
}
// 保证缩放过程中 照片位于界面中心
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2)
{
    //先获取imageview
    UIImageView *imview = [scrollView.subviews objectAtIndex:0];
    if (scrollView.zoomScale <= 1.0)
    {
        imview.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
    }
}
// 保证翻页后照片大小还原
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    scrollView = _slideScrollView;
    if (scrollView.contentOffset.x != 0) {
        if ((scrollView.contentOffset.x - _number) / self.view.bounds.size.width >= 0) {
            for (UIScrollView *scrollview in scrollView.subviews) {
                if ([scrollview isKindOfClass:[UIScrollView class]]) {
                    scrollview.zoomScale = 1.0;
                }
            }
            _number += self.view.bounds.size.width;
        }
    } else {
        for (UIScrollView *scrollview in scrollView.subviews) {
            if ([scrollview isKindOfClass:[UIScrollView class]]) {
                if (scrollview.zoomScale < 1.0) {
                    scrollview.zoomScale = 1.0;
                }
            }
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
